package com.tianji.remark.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.remark.pojo.dto.LikeRecordFormDTO;
import com.tianji.remark.pojo.po.LikedRecord;
import com.tianji.remark.mapper.LikedRecordMapper;
import com.tianji.remark.service.ILikedRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.tianji.common.constants.MqConstants.Exchange.LIKE_RECORD_EXCHANGE;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-11-09
 */
//@Service
@RequiredArgsConstructor
public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {

    private final RabbitMqHelper mqHelper;

    @Override
    public void addLikeRecord(LikeRecordFormDTO recordDTO) {
        // 点赞或取消点赞
        Boolean success = recordDTO.getLiked() ? like(recordDTO) : unLike(recordDTO);

        // 点赞失败直接返回
        if (!success) {
            return;
        }

        // 执行成功需要更新点赞数量
        Integer count = lambdaQuery().eq(LikedRecord::getBizId, recordDTO.getBizId()).count();

        mqHelper.send(LIKE_RECORD_EXCHANGE, StringUtils.format(LIKE_RECORD_EXCHANGE, recordDTO.getBizType())
                , new LikedTimesDTO(recordDTO.getBizId(), count));

    }

    private Boolean unLike(LikeRecordFormDTO recordDTO) {
        return remove(Wrappers.<LikedRecord>lambdaQuery().eq(LikedRecord::getBizId, recordDTO.getBizId())
                .eq(LikedRecord::getUserId, UserContext.getUser()));
    }

    private Boolean like(LikeRecordFormDTO recordDTO) {
        // 查询已有记录
        Integer count = lambdaQuery().eq(LikedRecord::getUserId, UserContext.getUser())
                .eq(LikedRecord::getBizId, recordDTO.getBizId())
                .count();

        if (count > 0) {
            // 只能点赞一次
            return false;
        }

        LikedRecord likedRecord = BeanUtils.copyBean(recordDTO, LikedRecord.class);
        likedRecord.setUserId(UserContext.getUser());
        return save(likedRecord);

    }

    @Override
    public Set<Long> isBizLiked(List<Long> bizIds) {

        // 判断业务ID与用户ID在点赞记录表中是否存在，存在表示点赞过，返回点赞的业务ID
        List<LikedRecord> recordList = lambdaQuery()
                .in(LikedRecord::getBizId, bizIds)
                .eq(LikedRecord::getUserId, UserContext.getUser())
                .list();

        return recordList.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
    }

    @Override
    public void readLikedTimesAndSendMessage(String bizType, int maxBizSize) {

    }
}
